﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AerolineaApp.Models;

namespace AerolineaApp.Controllers
{ 
    public class AerolineaController : Controller
    {
        private AerolineaBD db = new AerolineaBD();

        public ViewResult Index()
        {
            var aerolineas = db.Aerolineas.Include(a => a.Pais);
            return View(aerolineas.ToList());
        }

        public ViewResult Details(int id)
        {
            Aerolinea aerolinea = db.Aerolineas.Find(id);
            return View(aerolinea);
        }

        public ActionResult Create()
        {
            ViewBag.PaisId = new SelectList(db.Paises, "Id", "Descripcion");
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Aerolinea aerolinea)
        {
            if (ModelState.IsValid)
            {
                db.Aerolineas.Add(aerolinea);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PaisId = new SelectList(db.Paises, "Id", "Descripcion", aerolinea.PaisId);
            return View(aerolinea);
        }

        public ActionResult Edit(int id)
        {
            
            Aerolinea aerolinea = db.Aerolineas.Find(id);
            ViewBag.PaisId = new SelectList(db.Paises, "Id", "Descripcion", aerolinea.PaisId);
            return View(aerolinea);
        }

        [HttpPost]
        public ActionResult Edit(Aerolinea aerolinea)
        {
            if (ModelState.IsValid)
            {
                db.Entry(aerolinea).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PaisId = new SelectList(db.Paises, "Id", "Descripcion", aerolinea.PaisId);
            return View(aerolinea);
        }

        public ActionResult Delete(int id)
        {
            Aerolinea aerolinea = db.Aerolineas.Find(id);
            return View(aerolinea);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Aerolinea aerolinea = db.Aerolineas.Find(id);
            db.Aerolineas.Remove(aerolinea);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}